﻿using System;
using System.Collections.Generic;
using Cms.Domain.Attributes;

namespace Cms.Domain
{
    public class ContentTranslation : IAuditable
    {
        private Content _content;
        private ContentTranslationVersion _publishedVersion;
        private IList<ContentTranslationVersion> _versions;

        public ContentTranslation()
        {
            _versions = new List<ContentTranslationVersion>();
        }

        public virtual int ID { get; set; }

        public virtual DateTime? FirstPublished { get; set; }

        public virtual DateTime? LatestPublished { get; set; }

        public virtual Content Content
        {
            get { return _content; }
            set { _content = value; }
        }

        public virtual Locale Locale { get; set; }
        
        [GetStateFrom("ID")]
        public virtual ContentTranslationVersion PublishedVersion
        {
            get { return _publishedVersion; }
            set { _publishedVersion = value; }
        }
        
        public virtual IList<ContentTranslationVersion> Versions
        {
            get { return _versions; }
            set { _versions = value; }
        }
        
        public virtual string GetPublishedHtml()
        {
            return _publishedVersion != null ? _publishedVersion.Html : string.Empty;
        }
        
        public virtual string GetPublishedTitle()
        {
            return _publishedVersion != null ? _publishedVersion.Title : string.Empty;
        }
        
        public virtual string GetHtml(string timestamp)
        {
            ContentTranslationVersion ctv = GetVersion(timestamp);
            return ctv != null ? ctv.Html : string.Empty;
        }

        public virtual string GetTitle(string timestamp)
        {
            ContentTranslationVersion ctv = GetVersion(timestamp);
            return ctv != null ? ctv.Title : string.Empty;
        }
        
        public virtual ContentTranslationVersion GetVersion(string timestamp)
        {
            for (int i = _versions.Count - 1; i >= 0; i--)
                if (long.Parse(_versions[i].Timestamp) <= long.Parse(timestamp))
                    return _versions[i];
            return null;
        }
        
        public virtual void PublishVersion(string timestamp)
        {
            for (int i = _versions.Count - 1; i >= 0; i--)
                if (long.Parse(_versions[i].Timestamp) <= long.Parse(timestamp))
                {
                    _publishedVersion = _versions[i];
                    if (!_content.FirstPublished.HasValue)
                        _content.FirstPublished = DateTime.Now;
                    return;
                }
            _publishedVersion = null;
        }

        #region IAuditable Members

        public virtual string GetState()
        {

            return "{" +
                   "\"PublishedVersionID\": " + (_publishedVersion != null ? _publishedVersion.ID.ToString() : "null") + ", " +
                   "\"FirstPublished\": " + (FirstPublished.HasValue
                                                ? "\"" + FirstPublished.Value.ToShortDateString() + " " +
                                                  FirstPublished.Value.ToShortTimeString() + "\""
                                                : "null") + ", " +
                   "\"LatestPublished\": " + (FirstPublished.HasValue
                                                ? "\"" + FirstPublished.Value.ToShortDateString() + " " +
                                                  FirstPublished.Value.ToShortTimeString() + "\""
                                                : "null") +
                   "}";
        }

        #endregion
    }
}